package com.ruoyi.energy.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.energy.mapper.EmsAlarmRuleMapper;
import com.ruoyi.energy.domain.EmsAlarmRule;
import com.ruoyi.energy.service.IEmsAlarmRuleService;

/**
 * 告警规则Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-20
 */
@Service
public class EmsAlarmRuleServiceImpl implements IEmsAlarmRuleService {
    
    @Autowired
    private EmsAlarmRuleMapper emsAlarmRuleMapper;

    /**
     * 查询告警规则
     * 
     * @param ruleId 告警规则主键
     * @return 告警规则
     */
    @Override
    public EmsAlarmRule selectEmsAlarmRuleByRuleId(Long ruleId) {
        return emsAlarmRuleMapper.selectEmsAlarmRuleByRuleId(ruleId);
    }

    /**
     * 查询告警规则列表
     * 
     * @param emsAlarmRule 告警规则
     * @return 告警规则
     */
    @Override
    public List<EmsAlarmRule> selectEmsAlarmRuleList(EmsAlarmRule emsAlarmRule) {
        return emsAlarmRuleMapper.selectEmsAlarmRuleList(emsAlarmRule);
    }

    /**
     * 查询有效的告警规则列表
     * 
     * @return 告警规则集合
     */
    @Override
    public List<EmsAlarmRule> selectActiveAlarmRuleList() {
        return emsAlarmRuleMapper.selectActiveAlarmRuleList();
    }

    /**
     * 根据设备ID查询告警规则
     * 
     * @param deviceId 设备ID
     * @return 告警规则集合
     */
    @Override
    public List<EmsAlarmRule> selectAlarmRuleByDeviceId(Long deviceId) {
        return emsAlarmRuleMapper.selectAlarmRuleByDeviceId(deviceId);
    }

    /**
     * 新增告警规则
     * 
     * @param emsAlarmRule 告警规则
     * @return 结果
     */
    @Override
    public int insertEmsAlarmRule(EmsAlarmRule emsAlarmRule) {
        // 数据验证
        validateAlarmRule(emsAlarmRule);
        
        // 设置创建信息
        emsAlarmRule.setCreateBy(SecurityUtils.getUsername());
        emsAlarmRule.setCreateTime(new Date());
        
        return emsAlarmRuleMapper.insertEmsAlarmRule(emsAlarmRule);
    }

    /**
     * 修改告警规则
     * 
     * @param emsAlarmRule 告警规则
     * @return 结果
     */
    @Override
    public int updateEmsAlarmRule(EmsAlarmRule emsAlarmRule) {
        // 数据验证
        validateAlarmRule(emsAlarmRule);
        
        // 设置更新信息
        emsAlarmRule.setUpdateBy(SecurityUtils.getUsername());
        emsAlarmRule.setUpdateTime(new Date());
        
        return emsAlarmRuleMapper.updateEmsAlarmRule(emsAlarmRule);
    }

    /**
     * 批量删除告警规则
     * 
     * @param ruleIds 需要删除的告警规则主键
     * @return 结果
     */
    @Override
    public int deleteEmsAlarmRuleByRuleIds(Long[] ruleIds) {
        return emsAlarmRuleMapper.deleteEmsAlarmRuleByRuleIds(ruleIds);
    }

    /**
     * 删除告警规则信息
     * 
     * @param ruleId 告警规则主键
     * @return 结果
     */
    @Override
    public int deleteEmsAlarmRuleByRuleId(Long ruleId) {
        return emsAlarmRuleMapper.deleteEmsAlarmRuleByRuleId(ruleId);
    }

    /**
     * 修改规则状态
     * 
     * @param emsAlarmRule 告警规则
     * @return 结果
     */
    @Override
    public int updateAlarmRuleStatus(EmsAlarmRule emsAlarmRule) {
        return emsAlarmRuleMapper.updateAlarmRuleStatus(emsAlarmRule);
    }

    /**
     * 校验规则名称是否唯一
     * 
     * @param emsAlarmRule 告警规则
     * @return 结果
     */
    @Override
    public String checkRuleNameUnique(EmsAlarmRule emsAlarmRule) {
        Long ruleId = StringUtils.isNull(emsAlarmRule.getRuleId()) ? -1L : emsAlarmRule.getRuleId();
        EmsAlarmRule info = emsAlarmRuleMapper.checkRuleNameUnique(emsAlarmRule.getRuleName());
        if (StringUtils.isNotNull(info) && info.getRuleId().longValue() != ruleId.longValue()) {
            return "规则名称已存在";
        }
        return "0";
    }

    /**
     * 告警规则数据验证
     * 
     * @param emsAlarmRule 告警规则
     */
    private void validateAlarmRule(EmsAlarmRule emsAlarmRule) {
        if (StringUtils.isEmpty(emsAlarmRule.getRuleName())) {
            throw new RuntimeException("规则名称不能为空");
        }
        
        if (StringUtils.isEmpty(emsAlarmRule.getParamName())) {
            throw new RuntimeException("参数名称不能为空");
        }
        
        if (StringUtils.isEmpty(emsAlarmRule.getConditionType())) {
            throw new RuntimeException("条件类型不能为空");
        }
        
        if (StringUtils.isEmpty(emsAlarmRule.getAlarmLevel())) {
            throw new RuntimeException("告警级别不能为空");
        }
        
        // 根据条件类型验证阈值
        String conditionType = emsAlarmRule.getConditionType();
        if ("1".equals(conditionType) || "2".equals(conditionType) || "3".equals(conditionType)) {
            // 大于、小于、等于条件，需要设置阈值下限
            if (emsAlarmRule.getThresholdMin() == null) {
                throw new RuntimeException("当前条件类型需要设置阈值");
            }
        } else if ("4".equals(conditionType)) {
            // 区间条件，需要设置上下限
            if (emsAlarmRule.getThresholdMin() == null || emsAlarmRule.getThresholdMax() == null) {
                throw new RuntimeException("区间条件需要设置阈值上限和下限");
            }
            if (emsAlarmRule.getThresholdMin().compareTo(emsAlarmRule.getThresholdMax()) >= 0) {
                throw new RuntimeException("阈值下限必须小于上限");
            }
        }
        
        // 验证参数名称是否有效
        validateParamName(emsAlarmRule.getParamName());
        
        // 设置参数中文名
        setParamLabel(emsAlarmRule);
    }

    /**
     * 验证参数名称是否有效
     * 
     * @param paramName 参数名称
     */
    private void validateParamName(String paramName) {
        String[] validParams = {"voltage", "current", "power", "energy", "power_factor", "frequency", "temperature"};
        boolean isValid = false;
        for (String param : validParams) {
            if (param.equals(paramName)) {
                isValid = true;
                break;
            }
        }
        if (!isValid) {
            throw new RuntimeException("无效的参数名称");
        }
    }

    /**
     * 设置参数中文名
     * 
     * @param emsAlarmRule 告警规则
     */
    private void setParamLabel(EmsAlarmRule emsAlarmRule) {
        String paramName = emsAlarmRule.getParamName();
        String paramLabel = "";
        
        switch (paramName) {
            case "voltage":
                paramLabel = "电压";
                break;
            case "current":
                paramLabel = "电流";
                break;
            case "power":
                paramLabel = "功率";
                break;
            case "energy":
                paramLabel = "电能";
                break;
            case "power_factor":
                paramLabel = "功率因数";
                break;
            case "frequency":
                paramLabel = "频率";
                break;
            case "temperature":
                paramLabel = "温度";
                break;
            default:
                paramLabel = paramName;
                break;
        }
        
        emsAlarmRule.setParamLabel(paramLabel);
    }
} 